package com.bankcomm.ui.specialwidget;

import java.util.Hashtable;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Bitmap.Config;
import android.widget.ImageView;

import com.bankcomm.common.LPMid;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.rytong.tools.crypto.AESCipher;
import com.rytong.tools.crypto.DESCipher;
import com.rytong.tools.ui.Component;
import com.rytong.tools.ui.CssStyle;

public class LPQRCode extends Component {
  
    @Override
    public void initRealView(Activity activity, String value) {
        // TODO Auto-generated method stub
        realView_ = new MyQRCode(activity);
    }
	
	public class MyQRCode extends ImageView implements CompositedComponent{

		private int width;
		private int height;



		public MyQRCode(Context context) {
			super(context);
			setScaleType(ScaleType.FIT_XY);
			String strEncodeing=getPropertyByName("strEncoding");
			strEncodeing=DESCipher.doEncrypt(strEncodeing, "xytepfgn".getBytes());
			init(strEncodeing);
		}
		
		public void init(String strEncodeing) {
			try {
				Bitmap bm = Create2DCode(strEncodeing);
				setImageBitmap(bm);
			} catch (WriterException e) {
				e.printStackTrace();
			}
		}
		
		/**
		 * 用字符串生成二维码
		 * 
		 * @param str
		 * @throws WriterException
		 */
		public Bitmap Create2DCode(String str) throws WriterException {
			Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
			hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
			Object objWidth = cssStyle_.getProperty(CssStyle.WIDTH);
			Object objHeight = cssStyle_.getProperty(CssStyle.HEIGHT);
			width_ = Integer.parseInt(Component.fixedPX((String) objWidth));
			height_=Integer.parseInt(Component.fixedPX((String) objHeight));
			// 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
			BitMatrix matrix = new MultiFormatWriter().encode(str,BarcodeFormat.QR_CODE,width_ ,height_ , hints);
			width = matrix.getWidth();
			height = matrix.getHeight();
			// 二维矩阵转为一维像素数组,也就是一直横着排了
			int[] pixels = new int[width * height];
			for (int y = 0; y < height; y++) {
				for (int x = 0; x < width; x++) {
					if (matrix.get(x, y)) {
						pixels[y * width + x] = 0xff000000;
					}
				}
			}

			Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
			
			// 通过像素数组生成bitmap,具体参考api
			bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
			
			
			Bitmap newBmap=creatSaveBit(bitmap);
			LPMid.getInstance().QRBitMap = newBmap;
			return bitmap;
		}
		
		public Bitmap creatSaveBit(Bitmap bitmap){
			int w=width;
			int h=height;
				Bitmap newBmap=Bitmap.createBitmap(w, h, Config.ARGB_8888);
				Canvas canvas=new Canvas(newBmap);
				Bitmap bgBmap=Bitmap.createBitmap(w, h, Config.ARGB_8888);
				int[] pix=new int[w*h];
				for (int y = 0; y < h; y++) {
					for (int x = 0; x < w; x++) {
						int index=y*w+x;
					    int r=((pix)[index]>>16 & 0xff)|0xff;
					    int g=((pix)[index]>>8 & 0xff)|0xff;
					    int b=((pix)[index] & 0xff)|0xff;
					    pix[index]=0xff000000|(r<<16)|(g<<8)|b;
					}
				}
				bgBmap.setPixels(pix, 0, w, 0, 0, w, h);
				canvas.drawBitmap(bgBmap, 0, 0,null);
				canvas.drawBitmap(bitmap, 0, 0,null);
				canvas.save(Canvas.ALL_SAVE_FLAG);
				canvas.restore();
				return newBmap;
				
		}
		
		

		@Override
		public Component composited() {
			return LPQRCode.this;
		}
		
	}
	
	
}
